package com.example.demo.utils;

import com.example.demo.pojo.DataPojo;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.util.Bytes;

import java.util.ArrayList;
import java.util.List;

/**
 * @author WZH
 * @create 2021-09-19 14:12
 * @desc 数据处理、封装工具类
 **/
public class DataProcessUtils {

    public static List<DataPojo> getDataList(ResultScanner scanner) {
        //创建一个List用于保存数据
        List<DataPojo> dataList = new ArrayList<DataPojo>();
        for (Result result : scanner) {
            List<Cell> cellList = result.listCells();
            DataPojo dataPojo = new DataPojo();
            // 迭代单元格列表
            for (Cell cell : cellList) {
                // 打印列蔟名
                String cell_key = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
                String cell_value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());

                if ("sensorId".equals(cell_key)) {
                    dataPojo.setSensorId(cell_value);
                }
                if ("time".equals(cell_key)) {
                    dataPojo.setTime(cell_value);
                }
                if ("value".equals(cell_key)) {
                    dataPojo.setValue(String.format("%.3f", Float.valueOf(cell_value)));
                }
            }
            dataList.add(dataPojo);
        }
        return dataList;
    }


}
